26 您所在的位置:网站首页 openwrt 22端口 26

26

#26| 来源: 网络整理| 查看: 265

我们经常会在路由器上面配置端口转发的规则,用来访问内网机器的某个端口,openwrt上面有很多中实现端口转发的方式。 1、端口转发

比如我想用wan口的IP,192.168.2.180,远程连接lan口内网192.168.18.235的ubuntu,如何实现:

建立一个端口转发规则,将

2222端口,访问路由器内网192.168.18.235ubuntu的22端口,

在防火墙下面添加如下规则​

​cat /etc/config/firewall​

config redirect option enabled '1' option proto 'tcp' option comment 'ubuntu' option src_dport '2222' option dest_ip '192.168.18.235' option dest_port '22' option src 'wan' option target 'DNAT' option dest 'lan'

实际实现在iptable的nat表下面添加了如下规则

root@openwrt:/# iptables -t nat -nvL | grep 235 307 23589 zone_lan_prerouting all -- br-lan * 0.0.0.0/0 0.0.0.0/0 0 0 SNAT tcp -- * * 192.168.18.0/24 192.168.18.235 tcp dpt:22 /* @redirect[0] (reflection) */ to:192.168.18.1 307 23589 prerouting_lan_rule all -- * * 0.0.0.0/0 0.0.0.0/0 /* user chain for prerouting */ 0 0 DNAT tcp -- * * 192.168.18.0/24 192.168.2.180 tcp dpt:2222 /* @redirect[0] (reflection) */ to:192.168.18.235:22 0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:2222 /* @redirect[0] */ to:192.168.18.235:22

这样通过wan ip加src_dport就可以访问到内网机器的ssh端口了。

2、DMZ

dmz就是加强版的端口转发,比如你内网IP里面有22端口给ssh用,也有80端口给web用,也有23端口给telnet用。如果用端口转发的话,你需要建立三条端口转发的规则。用dmz的话一下子就都可以了。

在防火墙下面添加如下规则​

​cat /etc/config/firewall​

config redirect 'dmz' option name 'dmz' option src 'wan' option proto 'tcp' option target 'DNAT' option dest_ip '192.168.18.235' option enabled '1' config redirect 'dmzudp' option name 'dmzudp' option src_port '!67' option src 'wan' option proto 'udp' option target 'DNAT' option dest_ip '192.168.18.235' option enabled '1'

实际实现在iptable的nat表下面添加了如下规则

root@openwrt:/# iptables -t nat -nvL | grep 235 0 0 SNAT tcp -- * * 192.168.18.0/24 192.168.18.235 /* dmz (reflection) */ to:192.168.18.1 0 0 SNAT udp -- * * 192.168.18.0/24 192.168.18.235 /* dmzudp (reflection) */ to:192.168.18.1 0 0 DNAT tcp -- * * 192.168.18.0/24 192.168.2.180 /* dmz (reflection) */ to:192.168.18.235 0 0 DNAT udp -- * * 192.168.18.0/24 192.168.2.180 /* dmzudp (reflection) */ to:192.168.18.235 5 405 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 /* dmz */ to:192.168.18.235 26 2270 DNAT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp spt:!67 /* dmzudp */ to:192.168.18.23 3、Upnp

openwrt上面使用miniupnpd服务来实现upnp服务

root@zihome:# ps | grep mini 25503 root 1076 S /usr/sbin/miniupnpd -f /var/etc/miniupnpd.conf 28450 root 1520 S grep mini

/etc/config/upnpd里面的log_output设置成1,可以在logread下查看交互日志。

config upnpd 'config' option enable_natpmp '1' option enable_upnp '1' option secure_mode '1' option log_output '1' 开启upnp的时候: 打开控制面板->网络和 Internet->查看网络计算机和设备,可以看到路由器

右键查看属性可以看到路由器的信息

制造商 型号 设备网页 mac地址 唯一标识符 IP地址

抓包可以看到路由器发送SSDP广播包给239.255.255.250,NOTIFY消息内容包含上面信息。

关闭upnp的时候: 打开控制面板->网络和 Internet->查看网络计算机和设备,路由器丢失

抓包可以看到路由器发送SSDP广播包给239.255.255.250,NOTIFY消息内容被byebye。

upnp实现端口映射

upnp可以通过另一种方式实现端口映射,就是客户端告诉路由器说,我想要通过哪个端口应该到哪个端口,然后路由器帮忙建立端口转发规则。

ubuntu安装upnpc

sudo apt-get install -y miniupnpc

ubuntu的ssh添加2222端口,upnp默认只允许1024-65535端口的映射

config perm_rule option action 'allow' option ext_ports '1024-65535' option int_addr '0.0.0.0/0' option int_ports '1024-65535' option comment 'Allow high ports

sudo vim /etc/ssh/sshd_config

Port 22 Port 2222

sudo /etc/init.d/ssh restart

ubuntu 通过upnpc请求添加端口映射,2222内部端口,9000外部端口

upnpc -a 192.168.17.233 2222 9000 TCP

这时候在upnp.leases下面就会添加一条信息,iptable -t nat下面添加一条规则。完成端口映射

root@openwrt:/# cat /var/upnp.leases TCP:9000:192.168.17.233:2222:1655716845:libminiupnpc upnpc -d 192.168.17.233 2222 9000 TCP 当开启迅雷下载的时候 root@openwrt:/# cat /var/upnp.leases TCP:54366:192.168.17.151:53533:1655706396:PTL-000C29A6B88AGXGA UDP:54366:192.168.17.151:54366:1655706396:PTL-000C29A6B88AGXGA TCP:53532:192.168.17.151:53532:1655706396:PTL-000C29A6B88AGXGA root@openwrt:/# iptables -t nat -nvL Chain MINIUPNPD (1 references) pkts bytes target prot opt in out source destination 0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:63016 to:192.168.17.151:53866 0 0 DNAT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:63016 to:192.168.17.151:63016 0 0 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53864 to:192.168.17.151:53864


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有